import java.util.HashSet;

/**
 * 1726. 同积元组
 */
public class No1726 {
    public int tupleSameProduct(int[] nums) {
        int ans = 0;

        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }

        HashSet<Integer> dSet = new HashSet<>();

        int n = nums.length;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int a = nums[i];
                int b = nums[j];
                int ab = a * b;
                for (int k = j + 1; k < n; k++) {
                    int c = nums[k];
                    if (ab % c != 0 || dSet.contains(c)) continue;
                    int d = ab / c;
                    if (c == d) continue;
                    if (set.contains(d)) {
                        dSet.add(d);
                        ans += 8;
                    }
                }
                dSet.clear();
            }
        }
        return ans;
    }
}
